package com.eshore.norm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.eshore.norm.pojo.constant.Const;
import com.eshore.norm.pojo.dto.NormImage;
import com.eshore.norm.mapper.NormImageMapper;
import com.eshore.norm.pojo.po.NormImagePO;
import com.eshore.norm.service.INormImageService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.eshore.norm.util.DateUtils;
import com.eshore.norm.util.ListStringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * <p>
 * 镜像列表 服务实现类
 * </p>
 *
 * @author chenjl
 * @since 2024-01-05
 */
@Service
public class NormImageServiceImpl extends ServiceImpl<NormImageMapper, NormImage> implements INormImageService {

    @Override
    public void dataImport(NormImagePO po) {
        Optional<NormImagePO> po_optional = Optional.ofNullable(po);
        po_optional.ifPresent(s -> {
            Optional<List<NormImagePO.ImagesDTO>> images_optional = Optional.ofNullable(s.getImages());
            images_optional.ifPresent(item -> {
                List<String> newImagesIds = item.stream().map(NormImagePO.ImagesDTO::getId).collect(Collectors.toList());
                cleanOldData(Const.CONFIG_ID, newImagesIds);
                //将po转化成实体对象入库
                List<NormImage> normImages = toNormImage(Const.CONFIG_ID, images_optional);
                if (!CollectionUtils.isEmpty(normImages)) {
                    getBaseMapper().insertOrUpdateBatch(normImages);
                }
            });
        });
    }


    /**
     * 删除现已不存在的数据，保持同步一致
     *
     * @param configId
     * @param newImagesIds 需要同步的imagesId集合
     * @author v-chenjl62
     * @date 2024/1/10
     */
    private void cleanOldData(Long configId, List<String> newImagesIds) {
        List<String> oldimagesIds = lambdaQuery().eq(NormImage::getConfigId, configId).list().stream().map(NormImage::getImagesId).collect(Collectors.toList());
        //筛选出差异的imagesId即为多余的需要删除
        oldimagesIds.removeAll(newImagesIds);
        if(!CollectionUtils.isEmpty(oldimagesIds)) {
            getBaseMapper().delete(new QueryWrapper<NormImage>().in("images_id", oldimagesIds));
        }
    }

    private List<NormImage> toNormImage(Long configId, Optional<List<NormImagePO.ImagesDTO>> list_optional) {
        return list_optional.get().stream().map(item -> {
            NormImage entity = new NormImage();
            BeanUtils.copyProperties(item,entity, "id","tags");
            entity.setImagesId(item.getId());//imagesId 是唯一索引
            entity.setTags(ListStringUtils.listToString(item.getTags()));
            entity.setId(IdWorker.getId());
            entity.setConfigId(configId);
//            entity.setCreateDate(LocalDateTime.now());
//            entity.setUpdateDate(LocalDateTime.now());
            entity.setCreateDate(DateUtils.parse(item.getCreatedAt(),DateUtils.DATE_TIME_T_PATTERN));
            entity.setUpdateDate(DateUtils.parse(item.getUpdatedAt(),DateUtils.DATE_TIME_T_PATTERN));
            return entity;
        }).collect(Collectors.toList());
    }
}
